package com.sinodata.bsm.cicp.collector.sybase;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.AbstractJDBCCollector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.DBConnectionUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class Server extends AbstractJDBCCollector {

    private static final Logger logger = Logger.getLogger(Server.class);

    /*
     * 采集数据库基本信息,采集方式: 使用java.sql.DatabaseMetaData获得
     * 
     * @see com.union.pub.implement.Collector#collect(com.union.pub.vo.ResAttributeValValue[],
     *      java.lang.String)
     */

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        try {
            String[] propMetas = getPropMeta();
            //        String serverName = this.getParamMap().get("服务器名");
            String serverName = this.getParamMap().get("ServerName");
            String[] rs = this.getStrings(serverName, ip);

            ResPropertyValue propertyValueX = new ResPropertyValue();
            ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
            for (int i = 0; i < propMetas.length; i++) {
                propValues[i] = ResPropertyValue.createPropValue(rs[i], propMetas[i]);
            }
            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
        } catch (Exception ex) {
            logger.error("failed to collect sybase server", ex);
            throw new CollectorException(ex);
        }
        return values;
    }

    private String[] getStrings(String serverName, String ip) throws CollectorException {
        String[] propValArr = new String[5];
        Connection conn = null;
        try {
            conn = DBConnectionUtil.getInstance().getConnection("sybase", ip, serverName);
            String databaseProductName = "";
            String databaseProductVersion = "";
            String driverName = "";
            String driverVersion = "";
            if (conn != null) {// 判断是否建立连接
                DatabaseMetaData dmd = conn.getMetaData();
                if (dmd != null) {
                    databaseProductName = dmd.getDatabaseProductName();
                    databaseProductVersion = dmd.getDatabaseProductVersion();
                    driverName = dmd.getDriverName();
                    driverVersion = dmd.getDriverVersion();
                }
            }
            propValArr[0] = databaseProductName + "";
            propValArr[1] = databaseProductVersion + "";
            propValArr[2] = driverName + "";
            propValArr[3] = driverVersion + "";

        } catch (Exception ex) {
            logger.error("while connect database : ", ex);
            throw new CollectorException(ex);
        } finally {
            DBConnectionUtil.getInstance().returnConnection(conn);
        }
        return propValArr;
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "ServerName" };
    }

    @Override
    public String[] getPropMeta() {
        return new String[] { "DBProductName", "DBProductVersion", "DriverName", "DriverVersion" };
    }
}
